const moment = require('moment')
const md5 = require('md5')
class Users {
  constructor() { }
  // 展示用户
  async list(req, res) {
    let {pagesize,currentpage}=req.query
    pagesize = pagesize || 10
    currentpage = currentpage || 1
    let start=(currentpage-1)*pagesize
    let total=await req.db.query('select count(1) count from `users`',{type: req.db.QueryTypes.SELECT})
    let sql='select * from `users` limit '+start+','+pagesize
    let result = await req.db.query(sql,{type: req.db.QueryTypes.SELECT})
    res.send({
      error:0,
      message:'success',
      total:total[0].count,
      data:result
    })
  }
  // 用户添加 begin
  add(req, res) {
    let { username, password, email, phone } = req.body
    let ctime = moment().format('YYYY-MM-DD HH:m:s')
    let sql = 'insert into `users` (`username`,`password`,`email`,`phone`,`ctime`,`mtime`) values ("' + username + '","' + md5(password) + '","' + email + '","' + phone + '","' + ctime + '","' + ctime + '")'
    req.db.query(sql).then(result => {
      res.send({
        error: 0,
        message: 'success'
      })
    }).catch(error => {
      console.log(error)
      res.send({
        error: 10,
        message: 'insert fail'
      })
    })
  }
  // 用户添加 end

  // 验证用户名是否存在
  usernameExist(req, res) {
    let username = req.body.username
    if(req.body.oldname){
      let oldname=req.body.oldname
      if(username==oldname){
        res.send({
          error:0,
          message:'success',
          data:'false'
        })
        return
      }
    }
    let sql = 'select count(1) `count` from `users` where `username`=?'
    req.db.query(sql, { 
      replacements:[username],
      type: req.db.QueryTypes.SELECT
     }).then(result => {
      res.send({
        error: 0,
        message: 'success',
        data: result[0].count == 0 ? 'false' : 'true'
      })
    }).catch((error) => {
      console.log(error)
      res.send({
        error: 40,
        message: 'query fail'
      })
    })
  }

  // 验证手机是否被注册
  phoneExist(req, res) {
    let phone = req.body.phone
    if(req.body.oldphone){
      let oldphone=req.body.oldphone
      if(phone==oldphone){
        res.send({
          error:0,
          message:'success',
          data:'false'
        })
        return
      }
    }
    let sql = 'select count(1) `count` from `users` where `phone`=?'
    req.db.query(sql, { 
      replacements:[phone],
      type: req.db.QueryTypes.SELECT
    }).then(result => {
      res.send({
        error: 0,
        message: 'success',
        data: result[0].count == 0 ? 'false' : 'true'
      })
    }).catch((error) => {
      console.log(error)
      res.send({
        error: 40,
        message: 'query fail'
      })
    })
  }

    // 验证邮箱是否被注册
    emailExist(req, res) {
      let email = req.body.email
      if(req.body.oldemail){
        let oldemail=req.body.oldemail
        if(email==oldemail){
          res.send({
            error:0,
            message:'success',
            data:'false'
          })
          return
        }
      }
      let sql = 'select count(1) `count` from `users` where `email`=?'
      req.db.query(sql, { 
        replacements:[email],
        type: req.db.QueryTypes.SELECT
      }).then(result => {
        res.send({
          error: 0,
          message: 'success',
          data: result[0].count == 0 ? 'false' : 'true'
        })
      }).catch((error) => {
        console.log(error)
        res.send({
          error: 40,
          message: 'query fail'
        })
      })
    }
  
  // 用户登录时验证密码是否输入正确
  passwordLogin(req, res) {
    let { username, password } = req.body
    let sql = 'select * from `users` where `username`=? && `password`=?'
    req.db.query(sql, {replacements:[username,md5(password)], type: req.db.QueryTypes.SELECT }).then(result => {
      if (result.length == 0) {
        req.session.logined = false
        res.send({
          error: 40,
          message: 'query fail'
        })
      } else {
        req.session.logined = true
        req.session.loginInfo = result[0]
        res.send({
          error: 0,
          message: 'success',
          data: result
        })
      }

    }).catch(error=>{
      console.log(error)
      res.send({
        error:40,
        message:'query fail'
      })
    })
  }

  /* 个人资料 */
  async one(req, res) {
    if (req.session.logined == undefined) {
      res.send({
        error: 1,
        message: '请先登陆',
      })
      return
    }
    // let { id } = req.query
    let sql = 'select * from `users` where `id`=?'
    let result = await req.db.query(sql, { replacements: [req.session.loginInfo.id], type: req.db.QueryTypes.SELECT })
    res.send({
      error: 0,
      message: 'succes',
      data: result[0]
    })

  }

  // 用户信息
  info(req,res){
    res.send({
      error:0,
      message:'success',
      data:req.session.loginInfo
    })
  }
  // 注销登录
  logout(req,res){
    req.session.destroy(()=>{
      res.send({
        error:0,
        message:'ok'
      })
    })
  }
  // 用户信息的删除
  delete(req,res){
    let {id}=req.query
    let sql='delete from `users` where `id`="'+id+'"'
    req.db.query(sql).then(()=>{
      res.send({
        error:0,
        message:'success'
      })
    }).catch(error=>{
      console.log(error)
      res.send({
        error:20,
        message:'delete fail'
      })
    })
  }

  // 重置用户密码
  passwordReset(req,res){
    let {id}=req.query
    let sql='update `users` set `password`="'+md5('123456')+'" where `id`="'+id+'"'
    req.db.query(sql).then(()=>{
      res.send({
        error:0,
        message:'success'
      })
    }).catch(error=>{
      console.log(error)
      res.send({
        error:30,
        message:'update fail'
      })
    })
  }

  // 编辑用户信息
  edit(req,res){
    let {username,phone,email,id}=req.body
    let sql='update `users` set `username`="'+username+'",`phone`="'+phone+'", `email`="'+email+'" where `id`="'+id+'"'
    req.db.query(sql).then(()=>{
      res.send({
        error:0,
        message:'success'
      })
    }).catch(error=>{
      console.log(error)
      res.send({
        error:30,
        message:'update fail'
      })
    })
  }

  isexist(req,res) {
    let { fieldname, fieldvalue } = req.query
    let sql = 'select count(1) count from `classes` where `' + fieldname + '`="' + fieldvalue + '"'
    req.db.query(sql).spread((result) => {
      if (result[0].count === 0) {
        res.send({
          error: 0,
          message: '不存在'
        })
      } else {
        res.send({
          error: 1,
          message: '存在'
        })
      }
    })
  }
}
module.exports = new Users()